# -*- coding: utf-8 -*-
# @Time:2021/05/17 22:18
# @Author:Cindy


"""HTTP-specific events."""
import json

import mitmproxy.http
from mitmproxy import http
from mitmproxy.tools.main import mitmdump

from mitm import Counter
from recursion import recursion


class Events:

    def response(self, flow: mitmproxy.http.HTTPFlow):
        """
            The full HTTP response has been read.
        """
        if 'https://stock.xueqiu.com/v5/stock/batch/quote.json?_t' \
                in flow.request.url and 'x=' in flow.request.url:
            base_data = json.loads(flow.response.text)
            print(base_data)
            new_data = recursion(base_data, 3)
            print('*'*50)
            print(new_data)
            flow.response.text = json.dumps(new_data)


addons = [
    Counter(),
    Events()
]

if __name__ == '__main__':
    mitmdump(['-p', '8080', '-s', __file__])